package hw3.proxy.core;

import java.io.IOException;
import java.net.InetAddress;

import org.apache.http.HttpRequest;
import org.apache.http.impl.DefaultHttpServerConnection;

/**
 * This class represents an HTTP request that needs to be handled by the proxy server.
 * 
 * @author Adam Levi
 * @author Marina Skarbovsky
 *
 */
public class HttpRequestToHandle
{
	/**
	 * Constructor
	 * @param conn - DefaultHttpServerConnection with the client that sent the request
	 * @param request - the HTTP request as received from the client.
	 */
	public HttpRequestToHandle(DefaultHttpServerConnection conn, HttpRequest request)
	{
		m_Request = request;
		m_ConnectionToClient = conn;
	}
	
	/**
	 * @return DefaultHttpServerConnection with the client.
	 */
	public DefaultHttpServerConnection getConnectionToClient()
	{
		return m_ConnectionToClient;
	}
	
	/**
	 * @return HttpRequest as received from the client.
	 */
	public HttpRequest getRequest()
	{
		return m_Request;
	}
	
	/**
	 * closes the server's connection with the client.
	 */
	public void finalize() throws IOException
	{
		if (m_ConnectionToClient != null)
		{
			m_ConnectionToClient.close();
		}
	}
	
	/**
	 * @return the host name of the client that sent the request as string.
	 */
	public String getClientHostName()
	{
		if (m_ConnectionToClient != null)
		{
			InetAddress hostInetAddress = getConnectionToClient().getLocalAddress();
			return hostInetAddress.getHostName();
		}
		return null;
	}
	
	private DefaultHttpServerConnection m_ConnectionToClient;
	private HttpRequest m_Request;
}
